package com.smart.list.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.smart.list.dao.TaskDao;
import com.smart.list.entity.Task;

//@Repository("TaskDao")
public class TaskDaoJDBCimpl implements TaskDao {
	private static final Logger logger = LoggerFactory.getLogger(TaskDaoJDBCimpl.class);
	private Connection connection;
	
	public TaskDaoJDBCimpl() {
		super();
		String Username = "root";
		String Password = "admin";
		try {
			Class.forName("com.mysql.jdbc.Driver"); //Or any other driver
			connection = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/smartlist", Username, Password);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}


	@Override
	public boolean createTask(Task task) {		
		Date date = new Date(task.getCreated().getTime());
		boolean state = false;
		PreparedStatement pstmt = null;
		try {
			pstmt = connection.prepareStatement("INSERT INTO `smartlist`.`task` " +
					"(`task`, `completed`, `datecreated`) " +
					"VALUES ( ?, ?, ?);");
			pstmt.setString(1, task.getTask());
			pstmt.setBoolean(2, task.isStatus());
			pstmt.setDate(3, date);
			state = pstmt.execute();
			logger.info("Created Task: " + task.getTask());
		} catch (SQLException e1) {
			logger.error("Error in creating task " + e1);
			e1.printStackTrace();
		} finally{
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return state;		
	}

	@Override
	public Task readTask(long id) {
		ResultSet resultSet = null;
		PreparedStatement pstmt = null;
		Task task = null;
		
		try {
			pstmt = connection.prepareStatement("select * from `smartlist`.`task` where idTask=?;");
			pstmt.setLong(1, id);
			resultSet = pstmt.executeQuery();
			logger.info("Task: with id: " + id + " sucessufuly retrived");
			while (resultSet.next()) {
				// Read values using column name
				String title = resultSet.getString("task");
				boolean completed = resultSet.getBoolean("completed");
				java.util.Date date = resultSet.getDate("datecreated");
				task = new Task(title);
			}
		} catch (SQLException e1) {
			logger.error("Error in creating task " + e1);
			e1.printStackTrace();
		} finally{
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return task;
	}

//	private void closeConnection() {
//		try {
//			connection.close();
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		
//	}


	@Override
	public boolean updateTask(Task task) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteTask(Task task) {
		// TODO Auto-generated method stub
		return false;
	}

}
